<?php

class DbFactory {
	
	function DbFactory() {
	}
	
	static function makeFileInfo($arrResult=null) {
		return DbFactory::makeObject('data.logic.file.FileInfo', $arrResult);
	}
	static function makeFileStorage($arrResult=null) {
		return DbFactory::makeObject('data.logic.catalog.FileStorage', $arrResult);
	}
	function makeCategory($arrResult=null) {
		return DbFactory::makeObject('data.logic.catalog.Category', $arrResult);
	}
	static function makeRemoteStorage($arrResult=null) {
		return DbFactory::makeObject('data.logic.catalog.RemoteStorage', $arrResult);
	}
	static function makeUser($arrResult=null) {
		return DbFactory::makeObject('data.logic.user.User', $arrResult);
	}
	static function makeArticle($arrResult=null) {
		return DbFactory::makeObject('data.logic.article.Article', $arrResult);
	}
	
	// -- products
	
	static function getSingleLingvo() {
		$staticObjName = "staticLingvoObject";
		if (isset($GLOBALS[$staticObjName])) {
			return $GLOBALS[$staticObjName];
		} else {
			$className = 'data.logic.lingvo.Lingvo';
			$class = import($className);
			$obj = new $class();
			//$obj->setClassName($className);
			$GLOBALS[$staticObjName] = &$obj;
			return $obj;
		}
	}
	
	static function getLocale($lang_id=null) {
		import('data.logic.lingvo.Bundle');
		if (isset($GLOBALS[Bundle::KEY])) {
			if (!$lang_id) $lang_id = $GLOBALS[Bundle::KEY]->getId();
			return $GLOBALS[Bundle::KEY]->getInstant($lang_id);
		} else {
			return null;
		}
	}
	
	static function makeObject($className, $arrResult=null) {
		$class = import($className);
		$obj = new $class();
		//$obj->setClassName($className);
		if ($arrResult)
			$obj->fillEntity($arrResult);
		$obj->setLingvo(DbFactory::getSingleLingvo());
		$obj->setLocale(DbFactory::getLocale());
		return $obj;
	}	
}
?>